package powerUp;

import java.io.IOException;
import java.net.URL;

import mapControl.GraphicsMapControl;

import com.jme.scene.Node;
import com.jmex.audio.AudioSystem;
import com.jmex.audio.AudioTrack;
import com.model.md5.controller.MD5Controller;
import com.model.md5.importer.MD5Importer;
import com.model.md5.interfaces.IMD5Animation;
import com.model.md5.interfaces.IMD5Node;

public class BombPowerUp extends PowerUp {

	private GraphicsMapControl mapControl;
	private MD5Controller modelController;

	public BombPowerUp(int row, int col, GraphicsMapControl mapControl) {
		setRow(row);
		setCol(col);
		this.mapControl = mapControl;

		node = mapControl.getPhysicsSpace().createStaticNode();

		String dir = "data/model/potaBombPower/";
		URL md5mesh = getClass().getClassLoader().getResource(
				dir + "mesh.md5Mesh");

		URL animIdle = getClass().getClassLoader().getResource(
				dir + "idle.md5anim");

		MD5Importer importer = new MD5Importer();
		IMD5Node body = null;
		IMD5Animation idle = null;

		try {

			importer.loadMesh(md5mesh, "ModelNode");
			body = importer.getMD5Node();

			importer.loadAnim(animIdle, "idle");
			idle = importer.getAnimation();

		} catch (IOException e) {
			System.out
					.print("no se cargo bien algun dato del jugador, verificar la existencia y la correctitud de la ruta de los archivos .md5mesh y .md4anim");
		}

		modelController = new MD5Controller(body);
		modelController.addAnimation(idle);
		modelController.setActive(true);
		modelController.setRepeatType(1);
		modelController.setSpeed(1f);

		body.addController(modelController);
		Node model = (Node) body;

		/*
		 * Quaternion rot; rot = model.getLocalRotation();
		 * rot.fromAngleAxis((float) -Math.toRadians(90), Vector3f.UNIT_X);
		 */
		model.setLocalScale(.5f);
		model.setLocalTranslation(0, 1f, 0);
		node.attachChild(model);
		// node.setModelBound(new BoundingBox(new Vector3f(0, -8, 0), 4, 5, 4));
		node.generatePhysicsGeometry();

		// mapControl.getContadorBombas().instalarBomba();
		node.setLocalTranslation(mapControl.getTraslationCentrada(row, col));
		mapControl.getRootNode().attachChild(node);
		mapControl.getRootNode().updateRenderState();

	}

	@Override
	public void consumir() {
		AudioSystem audio = AudioSystem.getSystem();
		AudioTrack targetSound = audio.createAudioTrack(getClass().getResource(
        "/music/powerup.wav"), false);
		targetSound.setVolume(1.0f);	
		targetSound.play();
		mapControl.removePowerUp(this);
		mapControl.incBombPower();
	}

}
